<template>
  <el-dialog
    width="710px"
    :visible="show === 1"
    :append-to-body="true"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <div slot="title" class="title_close">
      <!-- 消息中心 -->
      <div class="title">{{$t('inccom.message_dialog.56hyspkspts0')}}</div>
      <img src="@/assets/img/icon_gb1.png" class="close" @click="hide" />
    </div>
    <div class="comment_list">
      <div class="comment_bar">
        <div
          class="comment_item"
          @click="checkNav(index)"
          v-for="(item, index) in list"
          :key="index"
          :class="navKey === index ? 'bt-blue' : 'bt-gray'"
        >
          <img
            class="comment_icon"
            :src="navKey === index ? item.img1 : item.img2"
            alt=""
          />
          <span>{{ item.name }}</span>
        </div>
      </div>
      <div class="comment_readed" @click="readAll">
        <img src="@/assets/img/icon_qd.png" alt="" />
        <!-- 一键全读 -->
        <span>{{$t('inccom.message_dialog.56hyspksqo80')}}</span>
      </div>
    </div>
    <!-- 评论消息列表 -->
    <!-- <div v-show="navKey === 0">
      <ul
        class="comment_content"
        v-infinite-scroll="load"
        infinite-scroll-disabled="commentBusy"
        infinite-scroll-distance="20"
      >
        <li v-for="(item, i) in commentList" :key="i">
          <div class="comment_detail">
            <img :src="headpic" alt="" />
            <div class="comment_item">
              <div class="comment_obj">
                <div class="comment_object">
                  <span>{{ nickname }}</span
                  >{{ `评论了你` }}
                </div>
                <div class="comment_time">{{ item.commentTime | date }}</div>
              </div>
              <div class="detail">{{ item.detail }}</div>
            </div>
          </div>
          <input
            v-model="item.replyContent"
            class="comment_input"
            type="textarea"
            :placeholder="$t('inccom.message_dialog.56hyspksqv40')"
          />
        </li>
      </ul>
    </div> -->
    <!-- 交易消息列表 -->
    <div v-show="navKey === 0">
      <ul
        class="message_content"
        v-infinite-scroll="loadTrade"
        infinite-scroll-disabled="tradeLoading"
        infinite-scroll-delay="200"
        infinite-scroll-distance="20"
      >
        <li v-for="(item, i) in tradeList" :key="i">
          <div class="message_time">{{ item.create_time | date }}</div>
          <div class="message_detail">{{ item.content }}</div>
        </li>
      </ul>
      <p class="more" v-if="tradeLoading && !tradefinished">{{ $t("m.Loading") }}</p>
      <p v-if="tradefinished" class="more">{{ $t("m.No_more") }}</p>
    </div>
    <!-- 系统通知列表 -->
    <div v-show="navKey === 1">
      <ul
        class="message_content"
        v-infinite-scroll="loadSystem"
        infinite-scroll-disabled="systemLoading"
        infinite-scroll-distance="20"
        infinite-scroll-delay="200"
      >
        <li v-for="(item, i) in systemList" :key="i">
          <div class="message_time">{{ item.create_time | date }}</div>
          <div class="message_detail">{{ item.content }}</div>
        </li>
      </ul>
      <p class="more" v-if="systemLoading && !systemfinished">{{ $t("m.Loading") }}</p>
      <p v-if="systemfinished" class="more">{{ $t("m.No_more") }}</p>
    </div>
  </el-dialog>
</template>

<script>
import {get_notice_list, post_notice_readAll} from '@/assets/js/API/user.js';
import dayjs from "dayjs";
export default {
  props: {
    show: Number,
  },
  filters: {
    date(val) {
      return dayjs(val * 1000).format("MM-DD    HH:mm");
    },
  },
  data() {
    return {
      list: [
        // {
        //   name: "消息提醒",
        //   img1: require("@/assets/img/icon_yjls.png"),
        //   img2: require("@/assets/img/icon_xxtxh.png"),
        // },
        {
          name: this.$t('inccom.message_dialog.56hyspksr0g0'),
          img1: require("@/assets/img/icon_jytxb.png"),
          img2: require("@/assets/img/icon_jytxh.png"),
        },
        {
          name: this.$t('inccom.message_dialog.56hyspksr580'),
          img1: require("@/assets/img/icon_xttzb.png"),
          img2: require("@/assets/img/icon_xttzh.png"),
        },
      ],
      navKey: 0, // 导航切换
      commentPage: 1, // 评论消息页数
      tradeList: [], // 交易消息
      tradePage: 1, // 交易消息
      systemList: [], // 系统通知
      systemPage: 1,
      commentBusy: false,
      tradeLoading: true, // 交易消息正在加载
      tradefinished: false, // 交易消息加载完成
      systemfinished: false,
      systemLoading: true,
    };
  },
  methods: {
    hide() {
      this.$emit("hides","1");
    },
    // 点击切换nav
    checkNav(index) {
      this.navKey = index;
      if (index === 0) {
        this.getMessage(2, this.tradePage);
      } else if (index === 1) {
        this.getMessage(3, this.systemPage);
      }
    },
    // 获取消息列表
    async getMessage(type, page) {
      if (type === 2) {
        if (!this.tradefinished) {
          const res = await get_notice_list({
              type,
              page,
            });
          if (res.code !== 1) return this.$message.error(res.msg);
          if (res.data.list.length > 0) {
            this.tradeList = [...this.tradeList, ...res.data.list];
            this.tradePage++;
          } else {
            this.tradefinished = true;
          }
          this.tradeLoading = false;
        }
      } else if (type === 3) {
        if (!this.systemfinished) {
          const res = await get_notice_list({
              type,
              page,
            });
          if (res.code !== 1) return this.$message.error(res.msg);
          if (res.data.list.length !== 0) {
            this.systemList = [...this.systemList, ...res.data.list];
            this.systemPage++;
          } else {
            this.systemfinished = true;
          }
          this.systemLoading = false;
        }
      }
    },
    // 滚动翻页
    load() {
      this.commentBusy = true;
      this.commentBusy = false;
    },
    loadTrade() {
      // busy == false时不执行滚动方法
      this.tradeLoading = true;
      this.getMessage(2, this.tradePage);
    },
    loadSystem() {
      this.systemLoading = true;
      this.getMessage(3, this.systemPage);
    },
    // 一键全读
    async readAll() {
      let type;
      // 交易消息
      if(this.navKey == 0) type = 2;
      else if(this.navKey == 1) type = 3;
      const res = await post_notice_readAll({
        type: type
      });
      if(res.code != 1) return this.$message.error(res.msg);
      this.$emit("getReadAll");
      if(this.navKey == 0) return this.$message.success(this.$t('inccom.message_dialog.56hyspksrgk0'));
      if(this.navKey == 1) return this.$message.success(this.$t('inccom.message_dialog.56hyspksrqo0'));
    }
  },
  computed: {
    nickname() {
      return this.$store.state.index.nickname;
    },
    headpic() {
      return this.$store.state.index.headpic;
    },
  },
};
</script>

<style lang="less">
@import "~@/assets/less/common.less";
.el-dialog {
  background-color: @sideColor;
  .el-dialog__body {
    padding: 0 20px 20px 20px;
  }
}
.title_close {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .title {
    font-size: 14px;
    color: @fff;
    line-height: 28px;
    text-align: center;
  }
  .close {
    width: 18px;
    height: 18px;
    cursor: pointer;
  }
}
.comment_list {
  display: flex;
  justify-content: space-between;
  padding-bottom: 16px;
  .comment_bar {
    display: flex;
    justify-content: space-between;
    margin-right: 30px;
    .comment_item {
      margin-right: 25px;
      cursor: pointer;
      .comment_icon {
        margin-right: 5px;
        height: 20px;
      }
    }
  }
  .comment_readed {
    display: flex;
    align-items: center;
    padding-right: 15px;
    cursor: pointer;
    img {
      height: 20px;
    }
    span {
      padding-left: 8px;
      font-size: 12px;
      color: @fbSix;
    }
  }
}
.comment_content {
  height: 350px;
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 3px;
    height: 1px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background-color: @LightBorderLine;
  }
  &::-webkit-scrollbar-track {
    border-radius: 3px;
    background: transparent;
  }
  .comment_detail {
    display: flex;
    align-items: center;
    img {
      margin-right: 20px;
      width: 50px;
      height: 50px;
      border: 1px solid transparent;
      border-radius: 50px;
    }
    .comment_item {
      width: 100%;
      .comment_obj {
        display: flex;
        line-height: 24px;
        align-items: center;
        justify-content: space-between;
        .comment_object {
          color: @fff;
          font-size: 14px;
          span {
            color: @hoverColor;
          }
        }
        .comment_time {
          color: @fbSix;
          font-size: 12px;
          padding-right: 12px;
        }
      }
      .detail {
        font-size: 12px;
        color: @fff;
        line-height: 20px;
      }
    }
  }
  .comment_input {
    background-color: @LightBorderLine;
    height: 40px;
    border: 1px solid transparent;
    margin: 10px 0 20px 60px;
    padding-left: 12px;
    border-radius: 3px;
    width: 88%;
  }
}
.message_content {
  height: 350px;
  overflow-y: auto;
  padding-right: 16px;
  &::-webkit-scrollbar {
    width: 3px;
    height: 1px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background-color: @LightBorderLine;
  }
  &::-webkit-scrollbar-track {
    border-radius: 3px;
    background: transparent;
  }
  .message_time {
    padding-top: 12px;
    padding-bottom: 6px;
    width: 100%;
    text-align: center;
    color: @fbSix;
    margin-right: 16px;
  }
  .message_detail {
    background-color: @LightBorderLine;
    color: @fff;
    font-size: 12px;
    line-height: 20px;
    padding: 15px 15px;
    margin-right: 16px;
  }
}
</style>
